package dq.study.nio.java.channel;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

/**
 * @author dongqun
 * @Description:
 * @date 2020/4/4 22:04
 */
public class ScatherAndGather {
    public static void main(String[] args) throws IOException {
        RandomAccessFile file = new RandomAccessFile("C:\\Users\\dongq\\Desktop\\1.txt","buffer");
        FileChannel channel = file.getChannel();
        CharBuffer buffer1 = CharBuffer.wrap("才食武昌鱼");
        CharBuffer buffer2 = CharBuffer.wrap("又饮长江水");
        CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
        ByteBuffer[] byteBuffers = {encoder.encode(buffer1),encoder.encode(buffer2)};
        channel.write(byteBuffers);
    }
}
